import java.util.Random;


public class Bot 
{
	BadugiClient bc;
	private int smallBlind, myTokens, power, countChangeCards, timeOfThink;
	private int[] goodCards, badCards;
	private String cards[];
	PowerOfCards poc=new PowerOfCards();
	private Random rand = new Random();
	Bot(BadugiClient bc)
	{
		this.bc=bc;
		timeOfThink=rand.nextInt(216)+1;
	}
	public void newDeal(int myTokens, int function, int smallBlind)
	{
		try
		{
			Thread.sleep(500+timeOfThink);
		} 
		catch (InterruptedException e)
		{
			System.out.println("Ktos obudzil bota, bot jest zly!");
		}
		this.smallBlind=smallBlind;
		if(function==2)
		{
			if(myTokens>smallBlind)
			{
				myTokens-=smallBlind;
				bc.bet(smallBlind);
			}
			else
			{
				myTokens=0;
				bc.allIn();
			}
		}
		else if(function==3)
		{
			if(myTokens>2*smallBlind)
			{
				myTokens-=2*smallBlind;
				bc.bet(2*smallBlind);
			}
			else
			{
				myTokens=0;
				bc.allIn();
			}
		}
		this.myTokens=myTokens;
	}
	//New Deal
	public void newDeal(int myTokens, int function)
	{
		try
		{
			Thread.sleep(500+timeOfThink);
		} 
		catch (InterruptedException e)
		{
			System.out.println("Ktos obudzil bota, bot jest zly!");
		}
		countChangeCards=0;
		if(function==2)
		{
			if(myTokens>smallBlind)
			{
				myTokens-=smallBlind;
				bc.bet(smallBlind);
			}
			else
			{
				myTokens=0;
				bc.allIn();
			}
		}
		else if(function==3)
		{
			if(myTokens>2*smallBlind)
			{
				myTokens-=2*smallBlind;
				bc.bet(2*smallBlind);
			}
			else
			{
				myTokens=0;
				bc.allIn();
			}
		}
		this.myTokens=myTokens;
	}
	
	//New Cards
	public void newCards(String newCards[])
	{
		if(!newCards[0].equals("0")) power=poc.countPower(newCards);
		goodCards=poc.getGoodCards();
		badCards=poc.getBadCards();
		cards=newCards;
	}
	//Change Time
	public void changeCards()
	{
		try
		{
			Thread.sleep(500+timeOfThink);
		} 
		catch (InterruptedException e)
		{
			System.out.println("Ktos obudzil bota, bot jest zly!");
		}
		countChangeCards++;
		int[] change = {0,0,0,0};
		for(int i=0; i<4-power; i++) change[badCards[i]]=1;
		bc.changeRequest(change);
	}
	//Move
	public void turn(int myBet, int maxBet)
	{
		try
		{
			Thread.sleep(500+timeOfThink);
		} 
		catch (InterruptedException e)
		{
			System.out.println("Ktos obudzil bota, bot jest zly!");
		}
		if(power<3)
		{
			if(myBet==maxBet)
			{
				bc.check();
			}
			bc.fold();
		}
		else if(power==3)
		{
			if(myBet==maxBet)
			{
				bc.check();
			}
			if(maxBet-myBet>myTokens/7)
			{
				bc.fold();
			}
			else
			{
				bc.call(maxBet);
				myTokens-=maxBet-myBet;
			}
		}
		else if(power==4)
		{
			try
			{
				if(Integer.parseInt(cards[goodCards[0]].substring(1))<=7)
				{
					if(Integer.parseInt(cards[goodCards[0]].substring(1))<=5)
					{
						if(myBet==maxBet)
						{
							if(maxBet+myTokens/(7/(countChangeCards+1))<myTokens)
							{
								bc.bet(maxBet+myTokens/(7/(countChangeCards+1)));
								myTokens-=(maxBet+myTokens/(7/(countChangeCards+1)))-myBet;
							}
							else
							{
								myTokens=0;
								bc.allIn();
							}
						}
						else if(maxBet-myBet>=myTokens)
						{
							myTokens=0;
							bc.allIn();
						}
						else
						{
							if(countChangeCards==3)
							{
								myTokens=0;
								bc.allIn();
							}
							else
							{
								if(maxBet+myTokens/(7/(countChangeCards+2))<myTokens)
								{
									bc.raise(maxBet+myTokens/(7/(countChangeCards+2)));
									myTokens-=(maxBet+myTokens/(7/(countChangeCards+2)))-myBet;
								}
								else
								{
									myTokens=0;
									bc.allIn();
								}
							}
						}
					}
					else
					{
						if(myBet==maxBet)
						{
							if(maxBet+myTokens/(7/(countChangeCards+1))<myTokens)
							{
								bc.bet(maxBet+myTokens/(7/(countChangeCards+1)));
								myTokens-=(maxBet+myTokens/(7/(countChangeCards+1)))-myBet;
							}
							else
							{
								bc.allIn();
								myTokens=0;
							}
						}
						else if(maxBet-myBet<myTokens/7)
						{
							if(maxBet+myTokens/7<myTokens)
							{
								bc.raise(maxBet+myTokens/7);
								myTokens-=(maxBet+myTokens/7)-myBet;
							}
							else
							{
								bc.allIn();
								myTokens=0;
							}
						}
						else if(maxBet-myBet==myTokens/7)
						{
							bc.call(maxBet);
							myTokens-=maxBet+myBet;
						}
						else if(maxBet-myBet>myTokens/7)
						{
							if(maxBet+myTokens/10<myTokens)
							{
								bc.raise(maxBet+myTokens/10);
								myTokens-=(maxBet+myTokens/10)-myBet;
							}
							else
							{
								bc.allIn();
								myTokens=0;
							}
						}
					}
				}
				else
				{
					if(myBet==maxBet)
					{
							bc.bet(maxBet+myTokens/(12/(countChangeCards+1)));
							myTokens-=(maxBet+myTokens/(12/(countChangeCards+1)))-myBet;
					}
					else if(maxBet-myBet<=myTokens/10)
					{
						bc.raise(myBet+myTokens/10);
						myTokens-=(myTokens/10);
					}
					else if(maxBet-myBet>=myTokens/5) bc.fold();
					else
					{
						bc.call(maxBet);
						myTokens-=maxBet-myBet;
					}
				}
			}
			catch(NumberFormatException ex)
			{
				System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe!");
			}
		}
	}
	
}
